import com.jly.mapper.OrderVOMapper;
import com.jly.mapper.ProxyUserMapper;
import com.jly.mapper.UserMapper;
import com.jly.mapper.UserVOMapper;
import com.jly.mapper.impl.UserMapperImpl;
import com.jly.po.User;
import com.jly.po.vo.OrderVO;
import com.jly.po.vo.UserVO;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import org.junit.Test;
import sun.swing.BakedArrayList;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Proxy;
import java.util.ArrayList;
import java.util.List;

public class testProxyDao {


    /**
     * 测试代理模式dao
     * @throws IOException
     */
    private static Logger logger = Logger.getLogger(testProxyDao.class.getName());
    @Test
    public void proxyDaoTest() throws IOException {

//            this.proxyDaoExample();
//            this.sqlDiyExample();
//        this.sqlDiyListExample();
//        this.resultMapTest();
//        this.resultOneToMoreTest();
        this.resultMoreToMoreTest();
    }

    public void resultMoreToMoreTest() throws IOException {

        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
        UserVOMapper mapper = sqlSession.getMapper(UserVOMapper.class);
        List<UserVO> all = mapper.findAllUserAndRole();
        sqlSession.close();
        for (UserVO vo :all) {
            System.out.println(vo);
        }
    }

    /**
     * 对于连表查询  返回自定义map测试 一个用户对应多个订单   一对多
     */
    public void resultOneToMoreTest() throws IOException {

        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
        UserVOMapper mapper = sqlSession.getMapper(UserVOMapper.class);
        List<UserVO> all = mapper.findAll();
        sqlSession.close();
        for (UserVO vo :all) {
            System.out.println(vo);
        }
    }
    /**
     * 对于连表查询  返回自定义map测试 一个订单对应一个用户   一对一
     */
    public void resultMapTest() throws IOException {

        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
        OrderVOMapper mapper = sqlSession.getMapper(OrderVOMapper.class);
        List<OrderVO> all = mapper.findAll();
        sqlSession.close();
        for (OrderVO vo :all) {
            System.out.println(vo);
        }
    }


    /**
     * 测试由代理模式 proxy代替传统的dao模式
     * @throws IOException
     */
    public void proxyDaoExample() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
        ProxyUserMapper mapper = sqlSession.getMapper(ProxyUserMapper.class);
        User user = mapper.findByUsername("纪联友");
        System.out.println(user);
        sqlSession.close();
    }

    /**
     * 自定义动态sql
     * @throws IOException
     */
    public void sqlDiyExample() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
        ProxyUserMapper mapper = sqlSession.getMapper(ProxyUserMapper.class);
        User userParams = new User();
        userParams.setId(1);
        userParams.setUsername("纪联友");
        List<User> byCondition = mapper.findByCondition(userParams);
        System.out.println(byCondition);
        sqlSession.close();
    }

    /**
     * sql 的foreach用法
     * @throws IOException
     */
    public void sqlDiyListExample() throws IOException {
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSession sqlSession = new SqlSessionFactoryBuilder().build(resourceAsStream).openSession();
        ProxyUserMapper mapper = sqlSession.getMapper(ProxyUserMapper.class);
        List ids = new ArrayList();
        ids.add(1);
        ids.add(3);
        List<User> userList = mapper.findByIds(ids);
        System.out.println(userList);
        sqlSession.close();
    }
}
